This rule is deprecated, and will eventually be removed.
A cookie’s domain specifies which websites should be able to read it. Left blank, browsers are supposed to only send the cookie to sites that
exactly match the sending domain. For example, if a cookie was set by lovely.dream.com, it should only be readable by that domain, and not by
nightmare.com or even strange.dream.com. If you want to allow sub-domain access for a cookie, you can specify it by adding a dot in
front of the cookie’s domain, like so: .dream.com. But cookie domains should always use at least two levels.
Cookie domains can be set either programmatically or via configuration. This rule raises an issue when any cookie domain is set with a single
level, as in .com.
Ask Yourself Whether
- the
domain
attribute has only one level as domain naming.
You are at risk if you answered yes to any of those questions.
Recommended Secure Coding Practices
- You should check the
domain
attribute has been set and its value has more than one level of domain nanimg, like:
sonarsource.com
Sensitive Code Example
setcookie("TestCookie", $value, time()+3600, "/~path/", ".com", 1); // Noncompliant
session_set_cookie_params(3600, "/~path/", ".com"); // Noncompliant
// inside php.ini
session.cookie_domain=".com"; // Noncompliant
Compliant Solution
setcookie("TestCookie", $value, time()+3600, "/~path/", ".myDomain.com", 1);
session_set_cookie_params(3600, "/~path/", ".myDomain.com");
// inside php.ini
session.cookie_domain=".myDomain.com";
See